<?php
class Product
{
	public function construct()
	{
		
	}
	
	public function index()
	{
		CommonFunc::loadFile(SMARTYLIBS . "Smarty.class.php");
		CommonFunc::loadFile("model/Product.php");
                CommonFunc::loadFile("model/Category.php");
		$smarty = new Smarty();
		$modProduct = new ModelProduct();
		$modCategory = new ModelCategory();
		$request = CommonFunc::getRequest();
                $catparam=$request['param1'];
                $catparamexpl=explode("-",$catparam);
                if($catparamexpl[0]=="cat"){
                    $catid=$catparamexpl[1];
                    $subcat_arr=$modCategory->getCategoryByParentId($catid);
                }
                elseif($catparamexpl[0]=="subcat"){
                    $subcatid=$catparamexpl[1];
                    $catid=$modCategory->getcategorybysubcatid($subcatid);
                    $subcat_arr=$modCategory->getCategoryByParentId($catid);
                }
                elseif($catparamexpl[0]=="searchkey"){
                    
                    $catid=$catparamexpl[2];
                    $subcatid=$catparamexpl[3];
                    $searchkey=$catparamexpl[1];
                }
          
		$cat_arr = $modCategory->listCategoriesfordropdown();
                $subcat_arr=$modCategory->getCategoryByParentId($catid);
                $catdisplaygetarray=$modCategory->listCategories();
                $catdisplayarr=array();
		foreach($catdisplaygetarray as $arr)
                {
                    $catdisplayarr[$arr["cat_id"]]=$arr["cat_desc"];
                }
                $statusarray=array(0=>"inactive",1=>"active");
             //   print_r($catdisplayarr);
                        $config=array();
			//$page =(int)$request['param1'];
                       // $limit = 'LIMIT 0, 500 ' ;
                        $perpage = 1000000000000;
                        $totalrowcount=$modProduct->gettotalrowcount($catid,$subcatid,$searchkey);
                        $curPage = ( ($request['param1']!=FALSE)? ( (int)$request['param1']/$perpage )+1:1 );
                        $limit = 'LIMIT  '.($curPage-1)*$perpage." , " . $perpage;
			$item_arr = $modProduct->listProducts("",$catid,$subcatid,$searchkey);
			$smarty->assign("title", "Product Management");
			$smarty->assign('item_arr', $item_arr);
			$paginatorHelper = new Adminpagination();
                        $config['base_url'] = "product/index/";
                        $config['total_rows'] = $totalrowcount;
                        $config['per_page'] = $perpage;
                        $config['cur_page'] = $curPage;
                        $paginatorHelper->initialize($config);
                        $paginatorhtml=$paginatorHelper->create_links();
                        $smarty->assign("cat_arr", $cat_arr);
                        $smarty->assign("subcat_arr", $subcat_arr);
                        $smarty->assign("catid", $catid);
                        $smarty->assign("searchkey", $searchkey);
                        $smarty->assign("subcatid", $subcatid);
                        $smarty->assign("paginatorhtml", $paginatorhtml);
                        $smarty->assign("catdisplayarr", $catdisplayarr);
                        $smarty->assign("statusarray", $statusarray);
			$smarty->display('product/index.tpl');
		
		
	}
	
	public function editProduct()
	{
		CommonFunc::loadFile(SMARTYLIBS . "Smarty.class.php");
		CommonFunc::loadFile(ADAPTOR . "/EditorFCK.php");	
		CommonFunc::loadFile('model/Category.php');	
		CommonFunc::loadFile('model/Product.php');	
		$smarty = new Smarty();
		$editorFCKHelper = new EditorFCKHelper();		
		$modCategory = new ModelCategory();
		$modPRoduct = new ModelProduct();
		$cat_arr = $modCategory->listCategoriesfordropdown();
		$request = CommonFunc::getRequest();
		$productid = $request['param1'];
		$product_arr = $modPRoduct->getProductByItemCode($productid);
		$smarty->assign("title", "Edit Product");
		$smarty->assign('objeditorFCK', $editorFCKHelper);
		$smarty->assign('product_arr', $product_arr);
		$smarty->assign("cat_arr", $cat_arr);
		$smarty->assign("productid", $productid);
		$smarty->display("product/editproduct.tpl");
	}

        public function addProduct()
	{
		CommonFunc::loadFile(SMARTYLIBS . "Smarty.class.php");
		CommonFunc::loadFile(ADAPTOR . "/EditorFCK.php");
		CommonFunc::loadFile('model/Category.php');
		CommonFunc::loadFile('model/Product.php');
		$smarty = new Smarty();
		$editorFCKHelper = new EditorFCKHelper();
		$modCategory = new ModelCategory();
		$modPRoduct = new ModelProduct();
		$cat_arr = $modCategory->listCategoriesfordropdown();
		$request = CommonFunc::getRequest();
		$itemcode = $request['param1'];
		$product_arr = $modPRoduct->getProductByItemCode($itemcode);
		$smarty->assign("title", "Add Product");
		$smarty->assign('objeditorFCK', $editorFCKHelper);
		$smarty->assign('product_arr', $product_arr);
		$smarty->assign("cat_arr", $cat_arr);
		$smarty->assign("itemcode", $itemcode);
		$smarty->display("product/addproduct.tpl");
	}

        function addFeatures()
        {
                CommonFunc::loadFile(SMARTYLIBS . "Smarty.class.php");
		CommonFunc::loadFile(ADAPTOR . "/EditorFCK.php");
		CommonFunc::loadFile('model/Category.php');
		CommonFunc::loadFile('model/Product.php');
		$smarty = new Smarty();
		$editorFCKHelper = new EditorFCKHelper();
		$modCategory = new ModelCategory();
		$modPRoduct = new ModelProduct();
		$cat_arr = $modCategory->listCategories();
		$request = CommonFunc::getRequest();
		$productid = $request['param1'];
                $catid=$modPRoduct->getcatidfromproductid($productid);
		$struct_arr = $modPRoduct->getFeaturesbyProductId($productid);
                $temp_struct_arr = $modPRoduct->listStructfields($catid);
                $productdet_arr=$modPRoduct->getProductByItemCode($productid);
                $temp_options="<option value=''>--Select--</option>";
                foreach($temp_struct_arr as $arr){
                    $temp_options.="<option value='".$arr["structkey"]."'>".$arr["structkey"]."</option>";
                }
                $action="controller.php?task=".base64_encode("savefeatures");
		$smarty->assign("title", "Add Product Features");
                $smarty->assign('formaction', $action);
		$smarty->assign('objeditorFCK', $editorFCKHelper);
		$smarty->assign('struct_arr', $struct_arr);
		$smarty->assign("cat_arr", $cat_arr);
                $smarty->assign("productdet_arr", $productdet_arr);
   
		$smarty->assign("productid", $productid);
                $smarty->assign("catid", $catid);
                $smarty->assign("temp_options", $temp_options);
		$smarty->display("product/addfeatures.tpl");
        }
	
	public function getSubCat()
	{
		CommonFunc::loadFile(SMARTYLIBS . "Smarty.class.php");
		CommonFunc::loadFile('model/Category.php');
		$smarty = new Smarty();
		$modCategory = new ModelCategory();
		$request = CommonFunc::getRequest();
		$cat_id = $request['cat_id'];
		if($cat_id != "")
		{
			$subcat_arr = $modCategory->getCategoryByParentId($cat_id);
		}
		
		$return_str = "";
		$return_str .= "<select name='subcat' id='subcat'>";
		$return_str .= "<option value=''>Select Sub Category</option>";
		if(count($subcat_arr) > 0)
		{
			foreach ($subcat_arr as $subcat_entry)
			{
				$return_str .= "<option value='" . $subcat_entry['cat_id'] . "'>" . $subcat_entry['cat_desc'] . "</option>";
			}
		}
		
		$return_str .= "</select>";
		
		echo $return_str;			
	}
	
	public function addProductImage()
	{
		CommonFunc::loadFile(SMARTYLIBS . "Smarty.class.php");	
		$smarty = new Smarty();
		CommonFunc::loadFile('model/Product.php');		
		
		$modProduct = new ModelProduct();
		$request = CommonFunc::getRequest();
		$productid = $request['param1'];
		$productimage_arr = $modProduct->getProductImages($productid);
                $productdet_arr=$modProduct->getProductByItemCode($productid);
                //print_r($productimage_arr);
		$smarty->assign("title", "Product Image Management");
		$smarty->assign("productimage_arr", $productimage_arr);
                $smarty->assign("productdet_arr", $productdet_arr);
		$smarty->assign("productid", $productid);		
		$smarty->display("product/addimage.tpl");
	}
	
	public function saveProduct()
	{
		CommonFunc::loadFile(SMARTYLIBS . "Smarty.class.php");		
		CommonFunc::loadFile('model/Product.php');		
		$smarty = new Smarty();
		$modProduct = new ModelProduct();
		$request = CommonFunc::getRequest();
               // print_r($request);exit;
		$modProduct->saveProduct($request);
	}
	
	public function homepageProducts()
	{
		CommonFunc::loadFile(SMARTYLIBS . "Smarty.class.php");
		CommonFunc::loadFile('model/Product.php');
		$smarty = new Smarty();
		$modProduct = new ModelProduct();
		$itemcode_arr = $modProduct->listhomepageproducts();
		$smarty->assign("title", "Homepage Products");
		$smarty->assign("itemcodearr", $itemcode_arr);
                $smarty->assign("siteurl", SITEURL);
		$smarty->display("product/homepageproduct.tpl");
	}
}	
?>
